package com.situ.web.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/servletPath")
public class ServletPathServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletContext servletContext = getServletContext();
		
		String aPath = servletContext.getRealPath("/WEB-INF/classes/a.txt");
//		E:\2345Downloads\Software\tomcat\apache-tomcat-8.5.42\webapps\JavaWeb1907\WEB-INF\classes\a.txt
		System.out.println(aPath);
		
		String bPath = servletContext.getRealPath("/b.txt");
//		E:\2345Downloads\Software\tomcat\apache-tomcat-8.5.42\webapps\JavaWeb1907\b.txt
		System.out.println(bPath);
		
		String cPath = servletContext.getRealPath("/WEB-INF/c.txt");
//		E:\2345Downloads\Software\tomcat\apache-tomcat-8.5.42\webapps\JavaWeb1907\WEB-INF\c.txt
		System.out.println(cPath);
		
//		String dPath = servletContext.getRealPath("../d.txt");
//      读取为null
//		System.out.println(dPath);
//		String dPath = servletContext.getRealPath("d.txt");
		String dPath = servletContext.getRealPath("/d.txt");
//      文件d.txt没有被配置到Tomcat中，虽然能读到路径，但读不到d.txt文件里面的内容
		System.out.println(dPath);
	}

}
